package contacts.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import contacts.dao.IRoomDao;
import contacts.dao.IStudentDao;
import contacts.dao.impl.RoomDaoImpl;
import contacts.dao.impl.StudentDaoImpl;
import contacts.po.Room;
import contacts.po.Student;

/**
 * @author yangliping
 *
 */
/**
 * @author yangliping
 *
 */
public class ContactsService {
	private IStudentDao studentDao = new StudentDaoImpl();
	private IRoomDao roomDao = new RoomDaoImpl();

	/**
	 * 添加联系人信息
	 * 
	 * @param student
	 */
	public void addStudent(Student student) {
		studentDao.save(student);
	}

	/**
	 * 修改联系人信息
	 * 
	 * @param i
	 * @param id
	 * @param student
	 */
	public void updateStudent(Student student) {
		studentDao.update(student);

	}

	/**
	 * 删除联系人信息
	 * 
	 * @param id
	 */
	public void deleteStudent(int id) {
		studentDao.delete(id);

	}

	/**
	 * 查询某一位联系人信息
	 * 
	 * @param id
	 * @return
	 */
	public Student getStudent(int id) {
		return studentDao.get(id);
	}

	/**
	 * 查询所有联系人信息
	 * 
	 * @return
	 */
	public List<Student> findAllStudent() {
		return studentDao.findAll();

	}

	/**
	 * 添加宿舍信息
	 * 
	 * @param student
	 */
	public void addRoom(Room room) {
		roomDao.save(room);
	}

	/**
	 * 修改宿舍信息
	 * 
	 * @param id
	 * @param student
	 */
	public void updateRoom(Room room) {
		roomDao.update(room);

	}

	/**
	 * 删除宿舍信息
	 * 
	 * @param id
	 */
	public void deleteRoom(int id) {
		roomDao.delete(id);

	}

	/**
	 * 查询某一个宿舍信息
	 * 
	 * @param id
	 * @return
	 */
	public Room getRoom(int id) {
		return roomDao.get(id);
	}

	/**
	 * 查询所有宿舍信息
	 * 
	 * @return
	 */
	public List<Room> findAllRoom() {
		return roomDao.findAll();

	}

	// 根据联系人编号查询联系人信息
	public List<Student> searchByStudentId(List<Student> studentList, Student student) {
		List<Student> result = new ArrayList<Student>(); // 查询结果集
		Collections.sort(studentList);
		int m = -1;
		while ((m = Collections.binarySearch(studentList, student, null)) >= 0) {
			Student bk = studentList.get(m);
			studentList.remove(m);
			result.add(bk);
		}
		return result;
	}

}